package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.CouponGift;
import com.burgeon.backend.SHOPBACKEND.entity.PlusActiveConfiguration;
import com.burgeon.backend.SHOPBACKEND.entity.PlusApplyRecord;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendPlusActiveMapper;
import com.burgeon.backend.SHOPBACKEND.service.BackendPlusActiveService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BackendPlusActiveServiceImpl implements BackendPlusActiveService {

    @Autowired
    private BackendPlusActiveMapper backendPlusActiveMapper;

    @Override
    public List<PlusApplyRecord> getData(String token,String flag,String startDate,String endDate,String phone ,String store,int start) {
        System.err.println(token);
        Integer count = backendPlusActiveMapper.getCount(token,flag,startDate,endDate,phone,store,start * 20);
        List<PlusApplyRecord> getData = backendPlusActiveMapper.getData(token,flag,startDate,endDate,phone,store,start * 20);
        for (PlusApplyRecord par:getData) {
            par.setY(count);
        }
        return getData;
    }

    @Override
    public void addPlusActive(PlusActiveConfiguration plusActiveConfiguration) {
           System.err.println(plusActiveConfiguration.getToken());
           if(plusActiveConfiguration.getToken()!=null && !plusActiveConfiguration.getToken().equals("")){
               Integer count = backendPlusActiveMapper.findPlusActive(plusActiveConfiguration.getToken());
               System.err.println(count);
               if(count!=0){
                   backendPlusActiveMapper.delPlusActive(plusActiveConfiguration.getToken());
               }
               System.err.println(plusActiveConfiguration.getIsEnable());
               if(plusActiveConfiguration.getIsEnable().equals("true")){
                   plusActiveConfiguration.setIsEnable("1");
               }else{
                   plusActiveConfiguration.setIsEnable("0");
               }
               int row = backendPlusActiveMapper.addPlusActive(plusActiveConfiguration);
               System.err.println(row);
               if (row == 0){
                   throw new ErrorDataException("出现错误");
               }
           }else {
               throw new ErrorDataException("出现错误");
           }
    }

    @Override
    public List<CouponGift> getCouponGift(String token) {
        List<CouponGift> data = backendPlusActiveMapper.getCouponGift(token);
        return data;
    }

    @Override
    public PlusActiveConfiguration showPlusActive(String token) {
        return backendPlusActiveMapper.showPlusActive(token);
    }
}
